package com.ruoyi.manage.cms.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StrUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import com.ruoyi.manage.cms.domain.GovernmentInfo;
import com.ruoyi.manage.cms.domain.vo.GovernmentInfoVo;
import com.ruoyi.manage.cms.service.IGovernmentInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
/**
 * 政府部门管理Controller
 *
 * @author ruoyi
 * @date 2025-07-27
 */
@Api(tags="政府部门管理控制器")
@RestController
@RequestMapping("/manage/government" )
public class GovernmentInfoController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IGovernmentInfoService governmentInfoService;

    /**
     * 查询政府部门管理列表
     */
    @ApiOperation(value = "查询政府部门管理",notes = "政府部门管理管理")
    @PreAuthorize("@ss.hasPermi('manage:government:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(GovernmentInfoVo governmentInfo,QueryWrapper<GovernmentInfo> queryWrapper) {
        setQueryCondition(governmentInfo,queryWrapper);
        startPage();
        List<GovernmentInfo> list = governmentInfoService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(GovernmentInfoVo governmentInfo, QueryWrapper<GovernmentInfo> queryWrapper) {
        if(StrUtil.isNotBlank(governmentInfo.getSerialNumber())){
            queryWrapper.eq(TABLE_PREFIX+"serial_number" ,governmentInfo.getSerialNumber());
        }
        if(StrUtil.isNotBlank(governmentInfo.getName())){
            queryWrapper.like(TABLE_PREFIX+"name" ,governmentInfo.getName());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出政府部门管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:government:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, GovernmentInfoVo governmentInfo,QueryWrapper<GovernmentInfo> queryWrapper) {
        if (CollUtil.isEmpty( governmentInfo.getIds())){
            setQueryCondition(governmentInfo,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",governmentInfo.getIds());
        }
        List<GovernmentInfo> list = governmentInfoService.list(queryWrapper);
        ExcelUtil<GovernmentInfo> util = new ExcelUtil<GovernmentInfo>(GovernmentInfo. class);
        util.exportExcel(response,list, "government");
    }

    /**
     * 获取政府部门管理详细信息
     */
    @ApiOperation(value = "获取政府部门管理",notes = "政府部门管理管理")
    @PreAuthorize("@ss.hasPermi('manage:government:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(governmentInfoService.queryGetById(id));
    }

    /**
     * 新增政府部门管理
     */
    @ApiOperation(value = "新增政府部门管理",notes = "政府部门管理管理")
    @PreAuthorize("@ss.hasPermi('manage:government:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody GovernmentInfo governmentInfo) {
        return toAjax(governmentInfoService.addGovernmentInfo(governmentInfo) ? 1 : 0);
    }

    /**
     * 修改政府部门管理
     */
    @ApiOperation(value = "修改政府部门管理",notes = "政府部门管理管理")
    @PreAuthorize("@ss.hasPermi('manage:government:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody GovernmentInfo governmentInfo) {
        return toAjax(governmentInfoService.updateGovernmentInfo(governmentInfo) ? 1 : 0);
    }

    /**
     * 删除政府部门管理
     */
    @ApiOperation(value = "删除政府部门管理",notes = "政府部门管理管理")
    @PreAuthorize("@ss.hasPermi('manage:government:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(governmentInfoService.deleteGovernmentInfoByIds(ids) ? 1 : 0);
    }
}
